package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.*;
import com.itheima.entity.SysUser;
import com.itheima.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/sys_user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    public R<PageDto> page(String login_name, Integer status, String phone, int page, int limit) {
        PageDto pageDto = sysUserService.pageList(login_name, status, phone, page, limit);
        return R.success(pageDto);

    }


    @PostMapping("/add")
    @CacheEvict(value = "indexCache", allEntries = true)
    public R<String> save(@RequestBody SysUserRoleDto sysUser) {
        String loginName = sysUser.getLoginName();
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
        qw.eq(StringUtils.isNotEmpty(loginName), SysUser::getLoginName, loginName);
        SysUser one = sysUserService.getOne(qw);
        if (one != null) {
            return R.error("该账户已被注册");
        }
        sysUser.setPassword(DigestUtils.md5DigestAsHex(sysUser.getPassword().getBytes()));
        List<String> list = Arrays.asList(sysUser.getRole_id());
        String string = list.toString();
        SysUser user = new SysUser();
        BeanUtils.copyProperties(sysUser, user);
        user.setRoleId(string);
        sysUserService.save(user);
        return R.success("添加成功");
    }

    @GetMapping("/del")
    public R<String> delete(String id) {
        LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper();
        qw.eq(id != null, SysUser::getId, id);
        sysUserService.remove(qw);
        return R.success("删除成功");
    }

    @GetMapping("/rowInfo")
    @CacheEvict(value = "indexCache", allEntries = true)
    public R<SysUserDtoes> get(String id) {
        SysUserDtoes userDto = sysUserService.get(id);
        return R.success(userDto);
    }

    @PostMapping("/update")
    // @CacheEvict(value = "indexCache", allEntries = true)
    public R<String> update(@RequestBody SysUserDtoes sysuser) {
//        SysUser sysUser = new SysUser();
//        BeanUtils.copyProperties(sysuser, sysUser);
//        List<String> role_id = sysuser.getRole_id();
//        String string = role_id.toString();
//        sysUser.setPassword(DigestUtils.md5DigestAsHex(sysuser.getPassword().getBytes(StandardCharsets.UTF_8)));
//        sysUser.setRoleId(string);
//        return R.success("修改成功");
//    }
       String name = sysuser.getLoginName();
       if (name != null) {
           LambdaQueryWrapper<SysUser> qw = new LambdaQueryWrapper<>();
           qw.eq(StringUtils.isNotEmpty(name), SysUser::getLoginName, name);
           SysUser one = sysUserService.getOne(qw);
           if (one == null) {
               return R.error("该账号不存在");
           }
           List<String> list = sysuser.getRole_id();
           System.out.println(list);
           SysUser sysUser = new SysUser();
           String role_id = list.toString();
           BeanUtils.copyProperties(sysuser, sysUser);
           sysUser.setRoleId(role_id);
           sysUserService.updateById(sysUser);
       }
       String id = sysuser.getId();
       LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(SysUser::getId, id);
       SysUser one = sysUserService.getOne(wrapper);
       one.setStatus(sysuser.getStatus());
       sysUserService.updateById(one);
       return R.success("修改成功");


   }
}
